package com.bdqn.bookshop.controller.mall;

import com.bdqn.bookshop.entity.Books;
import com.bdqn.bookshop.entity.Guide;
import com.bdqn.bookshop.entity.Setting;
import com.bdqn.bookshop.service.*;
import com.bdqn.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-07-24 20:55
 **/
@Controller
@RequestMapping("/mall")
public class MallViewController {

    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private BooksService booksService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private GuideService guideService;
    @Autowired
    private GarouselService garouselService;
    @Autowired
    private SettingService settingService;

    @RequestMapping("/{view}.html")
    public String index(@PathVariable("view") String view, Model model,Integer id,String bookName){
        model.addAttribute("setting",settingService.loadSetting());
        if(view.equals("publish")){
            model.addAttribute("classifyNode",classifyService.getClassifyNode());
            if(id!=null){
                model.addAttribute("book",booksService.selectById(id).getData());
            }
        }
        if(view.equals("detail")){
            if(id!=null){
                Books books = (Books) booksService.selectById(id).getData();
                model.addAttribute("book",books);
                model.addAttribute("alikeBook", booksService.alikeBook(books.getClassifyId()));
            }
        }
        if(view.equals("list")){
            model.addAttribute("classifyNode",classifyService.getClassifyNode());
            model.addAttribute("bookName",bookName);
        }
        if(view.equals("notice")){
            model.addAttribute("lifePeriod",noticeService.lifePeriod());
            model.addAttribute("newNotice",noticeService.newNotice());
        }
        if(view.equals("notice-detail")){
            model.addAttribute("newNotice",noticeService.newNotice());
            if(id!=null){
                model.addAttribute("notice",noticeService.getNoticeAndUpdateVisit(id));
            }
        }
        if(view.equals("help")){
            model.addAttribute("helpList",guideService.getGuideList());
            model.addAttribute("guide",guideService.getById(id==null?2:id));
        }
        if(view.equals("index")){
            model.addAttribute("newNotice",noticeService.newNotice());
            model.addAttribute("helpList",guideService.getFourGuieList());
            model.addAttribute("newBooks",booksService.newBooks());
            model.addAttribute("classifyNode",classifyService.getClassifyNode());
            model.addAttribute("garousels",garouselService.getGarousel());

        }
        return "mall/"+view;
    }
}
